#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/time.h>
 
#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "user_define.h"
#include "node_define.h"
#include "data_define.h"

#include "event_server.h"
// add para lib_include

char Buf[DIGEST_SIZE*4];

int event_server_init(void * sub_proc, void * para)
{
	int ret;
	// add yorself's module init func here
	return 0;
}

int event_server_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	// add yorself's module exec func here
	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(EVENT_DEFINE))&&(subtype==SUBTYPE(EVENT_DEFINE,EVENT_CONTROL)))
		{
			ret=proc_event_server_control(sub_proc,recv_msg);
		}
		else if((type==TYPE(EVENT_DEFINE))&&(subtype==SUBTYPE(EVENT_DEFINE,EVENT_REPORT)))
		{
			ret=proc_event_server_report(sub_proc,recv_msg);
		}
	}
	return 0;
}

int proc_event_server_control(void * sub_proc,void * recv_msg)
{
	int ret;
    DB_RECORD * db_record;
    void * type_message;
    MSG_EXPAND * msg_expand;

	RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	RECORD(NODE_DEFINE,USER) * node_user;
    BYTE uuid[DIGEST_SIZE];

	ret=message_get_record(recv_msg,&event_control,0);
	if(ret<0)
		return ret;
    // get name from login_input's name
    // the input info is user_name:role, while role can be SYSADMIN, USER,etc

    db_record = memdb_store(event_control,TYPE_PAIR(EVENT_DEFINE,EVENT_CONTROL),NULL);
    
    if(db_record != NULL)
    {
        type_message=create_type_message(TYPE_PAIR(EVENT_DEFINE,EVENT_CONTROL),NULL);
        if(type_message!=NULL)
            ex_module_sendmsg(sub_proc,type_message);
    }
    
    // check if it is a user_add control command
    if(event_control->command == CTRL_ADDUSER)
    {
        ret=message_get_define_expand(recv_msg,&msg_expand,TYPE_PAIR(NODE_DEFINE,USER));
        if(ret<0)
            return -EINVAL;
        if(msg_expand==NULL)
            return -EINVAL;
        node_user=msg_expand->expand;
        ret = memdb_comp_record_uuid(node_user,TYPE_PAIR(NODE_DEFINE,USER),uuid);
        if(ret<0)
            return ret;
        db_record = memdb_find(uuid,TYPE_PAIR(NODE_DEFINE,USER));
        if(db_record == NULL)
        {
            db_record = memdb_store(node_user,TYPE_PAIR(NODE_DEFINE,USER),NULL);
    
            if(db_record != NULL)
            {
                type_message=create_type_message(TYPE_PAIR(NODE_DEFINE,USER),NULL);
                if(type_message!=NULL)
                    ex_module_sendmsg(sub_proc,type_message);
    
            }
        }
        
    }
    
    ret = ex_module_sendmsg(sub_proc,recv_msg);
    return ret;
}

int proc_event_server_report(void * sub_proc,void * recv_msg)
{
	int ret;
    DB_RECORD * db_record;
    void * type_message;

	RECORD(EVENT_DEFINE,EVENT_REPORT) * event_report;

	ret=message_get_record(recv_msg,&event_report,0);
	if(ret<0)
		return ret;
    // get name from login_input's name
    // the input info is user_name:role, while role can be SYSADMIN, USER,etc

    db_record = memdb_store(event_report,TYPE_PAIR(EVENT_DEFINE,EVENT_REPORT),NULL);
    
    if(db_record != NULL)
        type_message=create_type_message(TYPE_PAIR(EVENT_DEFINE,EVENT_REPORT),NULL);
    if(type_message!=NULL)
        ex_module_sendmsg(sub_proc,type_message);
    ret = ex_module_sendmsg(sub_proc,recv_msg);
    return ret;
}
